<template>
  <basic-container>
    <avue-crud :option="option" :data="data" :page="page" ref="crud" @search-change="searchChange" @search-reset="searchReset"
      @current-change="currentChange" @size-change="sizeChange" @row-del="rowDel" @row-update="rowUpdate" @row-save="rowSave"
      @on-load="onLoad">
    </avue-crud>
  </basic-container>
</template>

<script>
  export default {
    data() {
      return {
        query: {},
        page: {
          pageSize: 10,
          currentPage: 1,
          total: 0
        },
        option: {
          searchMenuSpan: 4,
          border: true,
          index: true,
          refreshBtn:false,
          excelBtn:true,
          menuWidth: '150px',
          column: [{
              label: "客户编号",
              prop: "code",
              width: "150px",
              display: false,
              placeholder: "系统自动生成"
            },
            {
              label: "客户名称",
              prop: "name",
              searchPlaceholder: '编号&名称',
              search: true,
              rules: [{
                required: true,
                trigger: "blur"
              }]
            },
            {
              label: "电话",
              prop: "tel"
            },
            {
              label: "邮箱",
              prop: "email"
            },
            {
              label: "联系人",
              prop: "person"
            },
            {
              label: "地址",
              prop: "address"
            },
            {
              label: "备注",
              prop: "reamrk"
            }
          ]
        },
        data: []
      };
    },
    created() {
    },
    methods: {
      rowSave(row, loading, done) {
        this.request({
          url: '/api/BaseCustomer/add',
          method: 'post',
          data: row
        }).then(() => {
          loading();
          this.onLoad(this.page);
          done();
        }, error => {
          done();
          console.log(error);
        });
      },
      rowUpdate(row, index, loading, done) {
        return this.request({
          url: '/api/BaseCustomer/edit',
          method: 'post',
          data: row
        }).then(() => {
          loading();
          this.onLoad(this.page);
        }, error => {
          done();
          console.log(error);
        });
      },
      rowDel(row) {
        this.$confirm("确定数据删除?", {
            confirmButtonText: "确定",
            cancelButtonText: "取消",
            type: "warning"
          })
          .then(() => {
            return this.request({
              url: '/api/BaseCustomer/remove',
              method: 'post',
              params: {
                ids: row.id
              }
            });
          })
          .then(() => {
            this.onLoad(this.page);
          });
      },
      searchReset() {
        this.query = {};
        this.page.currentPage = 1;
        this.onLoad(this.page);
      },
      searchChange(params, done) {
        this.page.currentPage = 1;
        this.query = params;
        this.onLoad(this.page);
        done();
      },
      currentChange(currentPage) {
        this.page.currentPage = currentPage;
      },
      sizeChange(pageSize) {
        this.page.pageSize = pageSize;
      },
      onLoad(page) {
        this.request({
          url: '/api/BaseCustomer/getpages',
          method: 'get',
          params: {
            key: this.query.name == undefined ? '' : this.query.name,
            page: page.currentPage,
            limit: page.pageSize
          }
        }).then(res => {
          const data = res.data
          this.data = data.data;
          this.page.total = data.remark;
        });
      }
    }
  };
</script>

<style>
</style>
